﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace CxRomos.Core.Domain.Entities {
    public class Account {
        private readonly List<Permission> _permissions;

        public Account() {
            _permissions = new List<Permission>();
        }

        public virtual int AccountId { get; set; }
        public virtual DateTime? AgreedToTermsDate { get; set; }
        public virtual DateTime? BirthDate { get; set; }
        public virtual DateTime? CreateDate { get; set; }
        public virtual string Email { get; set; }
        public virtual Boolean EmailVerified { get; set; }
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
        public virtual DateTime? LastUpdateDate { get; set; }
        public virtual string Password { get; set; }
        public virtual int TermId { get; set; }
        public virtual string Username { get; set; }
        public virtual string Zip { get; set; }

        public virtual List<Permission> Permissions {
            get { return _permissions; }
        }

        public virtual bool HasPermission(string role) {
            return Permissions.Any(x => x.Name == role);
        }

        public virtual void AddPermission(Permission permission) {
            _permissions.Add(permission);
        }

        public override int GetHashCode() {
            var toReturn = base.GetHashCode();
            toReturn ^= AccountId.GetHashCode();
            return toReturn;
        }

        public override bool Equals(object obj) {
            if (obj == null) {
                return false;
            }
            var toCompareWith = obj as Account;
            return toCompareWith == null ? false : ((AccountId == toCompareWith.AccountId));
        }
    }
}